package com.xyz.graph.handler;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xyz.graph.pojo.gdata.edgeParam.AddEdgeParamPojo;
import com.xyz.graph.service.GDataService;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

/**
 * 数据入图
 * author：mxy
 */
@Slf4j
public class Edge2GraphServiceCallable implements Callable<String> {
    private final static ObjectMapper MAPPER = new ObjectMapper();

    private GDataService gDataService;
    //AddEdgeParamPojo
    private final List<AddEdgeParamPojo> datalist = new ArrayList<>();

    static {
        MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }
    @Override
    public String call() throws Exception {

        String [] str = Thread.currentThread().getName().split("-");
        Thread.currentThread().setName(str[0]+"-"+str[1]+"-关系入图-"+str[3]);

        log.info("edge 入图开始");
        datalist.stream().forEach(addEdgeParamPojo -> {
            log.info("边srcid：{}，dstid：{}",addEdgeParamPojo.getEntitySrcId(),addEdgeParamPojo.getEntityDstId());
            gDataService.addEdge(PreStartDoThing.getG(),addEdgeParamPojo);
        });
        datalist.clear();
        return "success";
    }

    public Edge2GraphServiceCallable(List<AddEdgeParamPojo> datalist,GDataService gDataService){
        //避免指令重排
        this.datalist.addAll(datalist);
        this.gDataService = gDataService;

    }
}
